clear all
set more off
set mem 100m
tempfile temp

/*

This dofile calculate voting turonver by region using EHANO data

*/

local data ../../../../../data/enaho
local working ../working

** 2007-2011
forvalues y = 2007/2011 {
	use `data'/`y'/enaho01b-`y'-1, clear

	gen year = `y'
	keep conglome vivienda hogar codperso year p15 p16 p17 p18 fac* ubigeo
	tab p15
	tab p16 
	tab p17
	
	drop if p17 == 1

	recode p15 (1 = 1) (2  = 0) , gen(turnout)
	recode p16 (1 = 0) (2 = 1) (3  = 0), gen(voted_blank)
	recode p16 (1 = 0) (2  = 0) (3 = 1), gen(voted_null)	
	recode p16 (1 = 1) (2 = 0) (3  = 0), gen(voted_valid)	
	recode p18 (1 = 1) (2 3 = 0), gen(vote_matters)
	gen voted_protest = voted_blank + voted_null
	
	destring conglome vivienda hogar codperso, replace
	keep conglome vivienda hogar codperso year vote* fac* ubigeo turn
	
	save dem_`y', replace
}

use dem_2007, clear
forvalues x = 2008/2011 {
	append using dem_`x'
}	

egen weight = rowtotal(factor07 facgob07)

gen dep = real(substr(ubigeo,1,2))

collapse (mean) voted_* turnout [fw=int(weight)], by(dep)

** SAVE
compress
save `working'/vote_enaho, replace

forvalues x = 2007/2011 {
	erase dem_`x'.dta
}	
